package com.paic.mhis.hcpms.agreement.protocol.biz.validator;

import java.text.ParseException;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.paic.mhis.common.util.DateUtil;
import com.paic.mhis.common.validator.ErrorEntry;
import com.paic.mhis.common.validator.HcpmsBaseValidator;
import com.paic.mhis.common.validator.MapValidator;
import com.paic.mhis.common.validator.ValidatorList;
import com.paic.mhis.hcpms.agreement.protocol.util.AgreementConstants;
import com.paic.mhis.hcpms.agreement.protocol.util.AgreementValidatorConstants;
import com.paic.pafa.app.biz.bo.validation.ValidStatus;

/**
 * Description: 协议管理模块操作校验
 * 
 * @param args
 * 
 *            Modification History: Date Version Author Description
 *            ------------------------------------------------------------------
 *            2014-5-4 hcpms_j2ee EX-LUORONG001 创建
 */
@Component("agreementValidator")
@Scope("prototype")
public class AgreementValidator extends HcpmsBaseValidator {
	@Override
	public Map<Object, Object> validate(Map<Object, Object> fieldMap) {
		validatorList.setValidateAll(true);

		// 常规验证
		validateFieldNotNull();
		validateDateFormat();
		validateFieldLengthLimit();

		ValidStatus validStatus = validatorList.validate(fieldMap);
		// 特殊字段验证
		validateOtherField(fieldMap, validStatus);

		return throwErrorMassage(validStatus);
	}

	/* 验证非空字段 */
	private void validateFieldNotNull() {
		validatorList
				.add(MapValidator
						.require(
								AgreementValidatorConstants.HEALTHCARE_PROVIDER_REQUIRE_VALIDATE,
								AgreementValidatorConstants.HEALTHCARE_PROVIDER_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				AgreementValidatorConstants.AGREEMENT_TYPE_REQUIRE_VALIDATE,
				AgreementValidatorConstants.AGREEMENT_TYPE_VALIDATE_MSG));
		validatorList
				.add(MapValidator
						.require(
								AgreementValidatorConstants.DATE_AGREEMENT_EFF_REQUIRE_VALIDATE,
								AgreementValidatorConstants.DATE_AGREEMENT_EFF_REQUIRE_VALIDATE_MSG));
		validatorList
				.add(MapValidator
						.require(
								AgreementValidatorConstants.DATE_AGREEMENT_END_REQUIRE_VALIDATE,
								AgreementValidatorConstants.DATE_AGREEMENT_END_REQUIRE_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				AgreementValidatorConstants.REGION_CODE_REQUIRE_VALIDATE,
				AgreementValidatorConstants.REGION_CODE_REQUIRE_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				AgreementValidatorConstants.PAYMENT_MODE_REQUIRE_VALIDATE,
				AgreementValidatorConstants.PAYMENT_MODE_REQUIRE_VALIDATE_MSG));
	}

	/* 验证长度限制字段 */
	private void validateFieldLengthLimit() {
		validatorList.add(MapValidator.maxLength(
				AgreementValidatorConstants.REMARKS_FROM_REQUIRE_LENGTH, 100,
				AgreementValidatorConstants.REMARKS_FROM_REQUIRE_LENGTH_MSG));
	}

	/* 验证日期字段 */
	private void validateDateFormat() {
		validatorList
				.add(MapValidator
						.date(AgreementValidatorConstants.DATE_AGREEMENT_EFF_REQUIRE_FORAMAT,
								AgreementValidatorConstants.DATE_AGREEMENT_EFF_REQUIRE_FORAMT_MSG));
		validatorList
				.add(MapValidator
						.date(AgreementValidatorConstants.DATE_AGREEMENT_END_REQUIRE_FORAMAT,
								AgreementValidatorConstants.DATE_AGREEMENT_END_REQUIRE_FORAMT_MSG));
	}

	/* 验证一些特殊字段 */
	private void validateOtherField(Map<Object, Object> fieldMap,
			ValidStatus validStatus) {
		String dateAgreementEff = (String) fieldMap
				.get(AgreementConstants.DATE_AGREEMENT_EFF);
		String dateAgreementEnd = (String) fieldMap
				.get(AgreementConstants.DATE_AGREEMENT_END);
		try {
			if (DateUtil.jsonDate2Date(dateAgreementEff).getTime() > DateUtil
					.jsonDate2Date(dateAgreementEnd).getTime()) {
				validStatus.addError(new ErrorEntry("",
						AgreementConstants.DATE_AGREEMENT_EFF,
						"协议生效日期不得大于协议终止日期"));
			}
		} catch (ParseException e) {
		}
	}

	public boolean addList(MapValidator mv) {
		validatorList = new ValidatorList();
		validatorList.add(mv);

		return true;
	}
}
